package cloud.zisu.springcloud.filter;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Date;

/**
 * @Desc: ""
 * @Author: caixiang
 * @DATE: 2020/9/17 11:22
 */
//注意一定要注入到spring容器中去
@Component
@Slf4j
public class MyLogGateWayFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("********************** come in MyLogGatewayFilter :"+new Date());
        String uname = exchange.getRequest().getQueryParams().getFirst("uname");
        if(uname == null){
            log.error("************用户名为null，非法");
            //给相应一个回复
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
            return exchange.getResponse().setComplete();
        }
        //把exchange 续传下去，，其实过滤器是整条过滤链
        return chain.filter(exchange);
    }

    //这个是 网关 加载过滤器的顺序，，order的顺序越小 优先级越高
    //这里填 0；； 就代表返回是 请求过来最先
    @Override
    public int getOrder() {
        return 0;
    }
}
